<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Document</title>
    <script src="vue.js"></script>
  </head>
  <body>
    <div id="root">
      <!--count前面加: 代表是整数 不加则代表字符串  -->
      <counter :count="1" @change="handleClick"></counter>
      <counter :count="2" @change="handleClick"></counter>
      <div>{{ total }}</div>
    </div>

    <script>
      var myCounter = {
        props: ['count'],
        data() {
          return {
            number: this.count
          };
        },
        template: '<div @click="handleClick">{{number}}</div>',
        methods: {
          handleClick: function() {
            //按照单向流规范，子组件不允许修改父组件传递的指，如果父组件传入的是对象，修改对象里面的内容会导致其他使用该对象的组件出现问题，
            //如果真的要改的，就需要拷贝一份作为自己的变量
            this.number++;
            this.$emit('change', 1);
          }
        }
      };

      var vm = new Vue({
        el: '#root',
        components: {
          counter: myCounter
        },
        data: {
          total: 3
        },
        methods: {
          handleClick: function(step) {
            this.total += step;
          }
        }
      });
    </script>
  </body>
</html>
